package com.shanghaionstar.notifysetting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.FlowPackageActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.notifysetting.presenter.NotifyPresenter;
import com.shanghaionstar.servicepackage.OnstarBuyPackageActivity;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * 车辆被盗提醒渠道设置
 * Created by Yonglang Hong on 2017/3/17.
 */
@ContentView(R.layout.activity_setnotify_an)
public class SetANINotifyActivity extends SwipeActivity implements IMVPView {
    private NotifyPresenter mPresenter;
    private static final int CHECK_SMS = 100;
    private static final int CHECK_APP = 101;
    private static final int CHECK_CALL = 102;

    @FindViewById
    private CustomLineLayout cll_setNotifySms, cll_setNotifyApp, cll_setNotifyPhone;

    @FindViewById
    private TextView tv_tipNotifySms, tv_tipNotifyApp, tv_tipNotifyPhone, tv_renewalTip, tv_statement, tv_statement_detail;

    @FindViewById
    private Button btn_renewal;
    @FindViewById
    private LinearLayout order_ll;
    @FindViewById
    private ProgressLayout progress_layout;
    @FindViewById
    private TopLayout top_layout;

    private NotifyConfigRes mNotifyConfig;

    private int whichCheck;
    private boolean isPreSelected;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NotifyPresenter(getScheduler());
        mPresenter.setView(this);
        initView();
    }

    private void initView() {
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.TANSetting_ClickBack.getStringValue());
                finish();
            }
        });
        Intent it = getIntent();
        if (it != null)
            mNotifyConfig = (NotifyConfigRes) it.getSerializableExtra(NotifyPresenter.IBType.TYPE_TAN);
        //正常
        if (mNotifyConfig != null ) {
            boolean notifySms = mNotifyConfig.getSms_checked().equalsIgnoreCase("1");
            cll_setNotifySms.setSelected(notifySms);
            SmsEmptyHelper helper = new SmsEmptyHelper(this,mNotifyConfig);
            helper.handleSms(tv_tipNotifySms,R.string.notify_tan_sms_tip);
            tv_tipNotifySms.setVisibility(notifySms ? View.VISIBLE : View.GONE);
//            tv_tipNotifySms, tv_tipNotifyApp, tv_tipNotifyPhone
            boolean notifyApp = mNotifyConfig.getMobile_checked().equalsIgnoreCase("1");
            cll_setNotifyApp.setSelected(notifyApp);
            tv_tipNotifyApp.setText(getString(R.string.notify_tan_app_tip));
            tv_tipNotifyApp.setVisibility(notifyApp ? View.VISIBLE : View.GONE);
            boolean notifyPhone = mNotifyConfig.getIscall().equalsIgnoreCase("1");
            cll_setNotifyPhone.setSelected(notifyPhone);
            tv_tipNotifyPhone.setText(String.format(getString(R.string.notify_tan_call_tip), CommonUtil.replaceString(mNotifyConfig.getPhone(), false)));
            tv_tipNotifyPhone.setVisibility(notifyPhone ? View.VISIBLE : View.GONE);


            cll_setNotifySms.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.TANSetting_SMSOFF.getStringValue():FunctionNameFindId.TANSetting_SMSON.getStringValue());
                    onRealCheckedChanged(buttonView, CHECK_SMS);
                }
            });
            cll_setNotifyApp.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.TANSetting_APPNotificationOFF.getStringValue():FunctionNameFindId.TANSetting_APPNotificationON.getStringValue());
                    onRealCheckedChanged(buttonView, CHECK_APP);
                }
            });
            cll_setNotifyPhone.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    BusinessUtil.sendReportByClickType(getApplication(),buttonView.isSelected()? FunctionNameFindId.TANSetting_MobileNotificationOFF.getStringValue():FunctionNameFindId.TANSetting_MobileNotificationON.getStringValue());
                    onRealCheckedChanged(buttonView, CHECK_CALL);
                }
            });
            tv_statement.setText(R.string.notify_statement);
            if (ApplicationUtil.getInstance().isGen10()) {
                tv_statement_detail.setText(R.string.notify_gen10_statement);
            } else {
                tv_statement_detail.setText(R.string.notify_gen9_statement);
            }
        } else {
            //过期情况
            cll_setNotifySms.setSelected(false);
            cll_setNotifySms.setSwitchEnable(false);
            cll_setNotifyApp.setSelected(false);
            cll_setNotifyApp.setSwitchEnable(false);
            cll_setNotifyPhone.setSelected(false);
            cll_setNotifyPhone.setSwitchEnable(false);
//            cll_setNotifyPhone.setVisibility(View.GONE);
            order_ll.setVisibility(View.VISIBLE);
            if (ApplicationUtil.getInstance().isExpiredSubscriber()) {
                tv_renewalTip.setText(R.string.notify_package_core_expired);
            } else {
                tv_renewalTip.setText(R.string.notify_package_data_expired);
            }
            btn_renewal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.TANSetting_ClickRenewPackage.getStringValue());
                    if (ApplicationUtil.getInstance().isExpiredSubscriber()) {
                        Intent i = new Intent(SetANINotifyActivity.this, OnstarBuyPackageActivity.class);
                        Bundle b = new Bundle();
                        b.putSerializable("packageType", PackageType.CORE);
                        i.putExtras(b);
                        startActivity(i);
                    } else {
                        Intent payMeIntent = new Intent(SetANINotifyActivity.this, FlowPackageActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("packageType", PackageType.DATA);
                        payMeIntent.putExtras(bundle);
                        startActivity(payMeIntent);
                    }
                }
            });

        }
    }

    private void onRealCheckedChanged(CompoundButton buttonView, int wh) {
        whichCheck = wh;
        isPreSelected = buttonView.isSelected();
        buttonView.setSelected(!isPreSelected);
        switch (wh) {
            case CHECK_SMS:
                mNotifyConfig.setSms_checked(!isPreSelected ? "1" : "0");
                break;
            case CHECK_CALL:
                mNotifyConfig.setIscall(!isPreSelected ? "1" : "0");
                break;
            case CHECK_APP:
                mNotifyConfig.setMobile_checked(!isPreSelected ? "1" : "0");
                break;
        }
        mPresenter.setNotifyConfig(mNotifyConfig);
    }


    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }


    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        onSetConfigFailed();
    }

    @Override
    public void onReponseSuccess(Object object) {
        if (object != null && object instanceof BaseResponse) {
            BaseResponse response = (BaseResponse) object;
            if (!response.code.equalsIgnoreCase("E0000")) {
                onSetConfigFailed();
            } else {
                onSuccessLayout();
            }
        }
    }

    private void onSetConfigFailed() {
        switch (whichCheck) {
            case CHECK_SMS:
                cll_setNotifySms.setSelected(isPreSelected);
                mNotifyConfig.setSms_checked(isPreSelected ? "1" : "0");
                tv_tipNotifySms.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
            case CHECK_CALL:
                cll_setNotifyPhone.setSelected(isPreSelected);
                mNotifyConfig.setIscall(isPreSelected ? "1" : "0");
                tv_tipNotifyPhone.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
            case CHECK_APP:
                cll_setNotifyApp.setSelected(isPreSelected);
                mNotifyConfig.setMobile_checked(isPreSelected ? "1" : "0");
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
        }
    }

    private void onSuccessLayout() {
        switch (whichCheck) {
            case CHECK_APP:
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
            case CHECK_SMS:
                tv_tipNotifySms.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
            case CHECK_CALL:
                tv_tipNotifyPhone.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }


}

